<template>
  <div class='sub-category'>
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter  @filter-change='filterChange' />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @change-sort='sortChange' />
        <!-- 列表 -->
        <ul>
          <li v-for="item in list" :key="item.id" >
            <GoodsItem :goods="item" />
          </li>
        </ul>
        <!-- 加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData" />
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item'
import { findSubCategoryGoods } from '@/api/category'
import { reactive, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    const route = useRoute()
    // // 单次接口调用的状态
    const loading = ref(false)
    // // 加载完成的标志
    const finished = ref(false)
    // // 商品总数
    const total = ref(0)
    // 商品列表
    const list = ref([])

    // 请求参数
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      categoryId: route.params.id
    })

    // 接收筛选组件传过来的数据
    const filterChange = (params) => {
      // 把sortParams对象中的所有属性放到reqParams对象里面
      Object.assign(reqParams, params)
      // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)

      reqParams.page = 1
      loading.value = false
      finished.value = false
      total.value = 0
      if (list.value.length === 0) {
        getData()
      } else {
        list.value = []
      }
    }

    // 接收排序组件传递过来的参数
    const sortChange = (sortParams) => {
      // ES6的方法Object.assign，用于合并两个对象中的属性(浅拷贝)
      // 把sortParams对象中的所有属性放到reqParams对象里面
      Object.assign(reqParams, sortParams)
      // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)
      if (list.value.length === 0) {
        getData()
      } else {
        list.value = []
      }
      reqParams.page = 1
      loading.value = false
      finished.value = false
      total.value = 0
    }

    // 发送请求
    const getData = () => {
      loading.value = true
      findSubCategoryGoods(reqParams).then(ret => {
        list.value.push(...ret.result.items)
        // 获取商品列表的总数
        total.value = ret.result.counts
        // 本次接口调用完成
        loading.value = false
        if (list.value.length === total.value && total.value !== 0) {
          // 没有更多数据了
          finished.value = true
        }
        // 页码需要累加
        reqParams.page += 1
      })
    }

    // 如果二级分类发生变化，重置当前的数据
    watch(() => route.params.id, (newVal) => {
      // 判断是否为二级分类
      if (route.fullPath !== '/category/sub/' + newVal) return
      // 重置数据
      list.value = []
      total.value = 0
      loading.value = false
      finished.value = false
      reqParams.page = 1
    })

    return { loading, finished, getData, list, sortChange, filterChange }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
